﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using Microsoft.Win32;

using SinsMapPacker;

namespace SinsMapPacker.Installer
{

class App
{
    public static void Main(string[] args)
    {
        if (args.Length == 0)
            return;
        
        if (!File.Exists(args[0]))
            return;

        var mapPack = MapPack.Open(args[0]);

        // Determine target folder of SoASE.
        // HKEY_LOCAL_MACHINE\SOFTWARE\Stardock\Drengin.net\sins\:Path
        var registryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Stardock\Drengin.net\sins");
        if (registryKey == null)
        {
            MessageBox.Show("Sins of a Solar Empire does not appear to be installed!", "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        var targetDir = (string)registryKey.GetValue("Path");
        if (targetDir == null)
        {
            MessageBox.Show("Can't find Sins of Solar Empire path in registry", "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        // Verify
        var prompt = string.Format(
            "This will install Map Pack '{0}' to {1}. Are you sure you want to continue?",
            Path.GetFileName(args[0]), targetDir);
        if (MessageBox.Show(prompt, "Install Map Pack",
            MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != DialogResult.Yes)
            return;

        // Unpack it
        mapPack.Unpack(targetDir);
    }
}

}
